package com.xpec.c4.service.module.h5gateway;

import java.util.Map;

import com.xpec.c4.h5net.H5XSEPacketCategoryMeter;
import com.xpec.c4.h5net.H5XSEPacketCategoryMeter.PacketStat;

public class H5GatewayStat implements H5GatewayStatMBean  
{
	@Override
	public String getInputPackets() 
	{
		Map<Integer,PacketStat> map = H5XSEPacketCategoryMeter.getInstance().getInputPackets();
		Long t = H5XSEPacketCategoryMeter.getInstance().getLastResetTime();
		String result = (t == null ? "N/A" : t) + "\n" + print(map);
		return result;
	}
	
	@Override
	public String getOutputPackets() 
	{
		Map<Integer,PacketStat> map = H5XSEPacketCategoryMeter.getInstance().getOutputPackets();
		Long t = H5XSEPacketCategoryMeter.getInstance().getLastResetTime();
		String result = (t == null ? "N/A" : t) + "\n" + print(map);
		return result;
	}

	private String print(Map<Integer, PacketStat> map) 
	{
		StringBuilder sb = new StringBuilder();
		for(Integer key : map.keySet())
		{
			PacketStat stat = map.get(key);
			
			sb.append(key + "=> ");
			sb.append("p:" + stat.getPackets() + " / ");
			sb.append("b:" + stat.getBytes() + " / ");
			sb.append("a:" + stat.getBytes().get()/stat.getPackets().get() + "\n");
		}		
		return sb.toString();
	}
}
